<template>
    <div ref="captcha" class="captcha" style="position: relative">
            <div class="captcha-canvas"></div>
            <div class="refreshIcon"></div>
            <div class="sliderContainer">
                <div class="sliderMask">
                    <div class="slider"><span class="sliderIcon"></span></div>
                </div>
                <span class="sliderText">{{sliderBarTip}}</span>
            </div>
    </div>
</template>
<script>
import SliderCaptcha from "../../../assets/js/captcha.js";
import http from '../../../assets/js/http.js'
export default {
  name : "dx-captcha",
  props : {
      serverKey : {type:String},
      sliderBarTip : {type:String,default:'向右滑动滑块填充拼图'}
  },

  data() {
    return {};
  },
  methods: {
      refresh(){
        SliderCaptcha.reset();
      },
      getSeedValue(){
          return new Promise((resolve,reject)=>{
                this.apiGet('login/getRandSeed?sid='+this.serverKey).then(resp=>{
                    if(resp.code==200){
                        resolve(resp.data);
                    }
                }).catch((error)=>{
                    reject(error);
                });
          });
      }
  },
  created() {
 
  },
  mounted() {
        this.$nextTick(()=>{
                SliderCaptcha.init(this.$refs.captcha,this.getSeedValue,(randSeed)=>{
                    // document.getElementById('msg').innerHTML = '登录成功！'
                    this.$emit('success',randSeed);
                },()=>{
                    this.$emit('fail'); 
                });
        });
      
  },
  mixins : [http]
};
</script>
<style scoped>

.captcha a:link,
.captcha a:visited,
.captcha a:hover,
.captcha a:active {
  margin-left: 100px;
  color: #0366d6;
}
/* .captcha .captcha-block {
  position: absolute;
  left: 0;
  top: 0;
} */

.captcha .sliderContainer {
  position: relative;
  text-align: center;
  width: 310px;
  height: 40px;
  line-height: 40px;
  margin-top: 15px;
  background: #f7f9fa;
  color: #45494c;
  border: 1px solid #e4e7eb;
}

.captcha .sliderContainer_active .slider {
  height: 38px;
  top: -1px;
  border: 1px solid #1991fa;
}

.captcha .sliderContainer_active .sliderMask {
  height: 38px;
  border-width: 1px;
}

.captcha .sliderContainer_success .slider {
  height: 38px;
  top: -1px;
  border: 1px solid #52ccba;
  background-color: #52ccba !important;
}

.captcha .sliderContainer_success .sliderMask {
  height: 38px;
  border: 1px solid #52ccba;
  background-color: #d2f4ef;
}

.captcha .sliderContainer_success .sliderIcon {
  background-position: 0 0 !important;
}

.captcha .sliderContainer_fail .slider {
  height: 38px;
  top: -1px;
  border: 1px solid #f57a7a;
  background-color: #f57a7a !important;
}

.captcha .sliderContainer_fail .sliderMask {
  height: 38px;
  border: 1px solid #f57a7a;
  background-color: #fce1e1;
}

.captcha .sliderContainer_fail .sliderIcon {
  background-position: 0 -83px !important;
}
.captcha .sliderContainer_active .sliderText,
.captcha .sliderContainer_success .sliderText,
.captcha .sliderContainer_fail .sliderText {
  display: none;
}

.captcha .sliderMask {
  position: absolute;
  left: 0;
  top: 0;
  height: 40px;
  border: 0 solid #1991fa;
  background: #d1e9fe;
}

.captcha .slider {
  position: absolute;
  top: 0;
  left: 0;
  width: 40px;
  height: 40px;
  background: #fff;
  box-shadow: 0 0 3px rgba(0, 0, 0, 0.3);
  cursor: pointer;
  transition: background 0.2s linear;
}

.captcha .slider:hover {
  background: #1991fa;
}

.captcha .slider:hover .sliderIcon {
  background-position: 0 -13px;
}

.captcha .sliderIcon {
  position: absolute;
  top: 15px;
  left: 13px;
  width: 14px;
  height: 10px;
  background: url(~assets/images/captcha_bg.png) 0 -26px;
  background-size: 34px 471px;
}

.captcha .refreshIcon {
  position: absolute;
  /* right: 0; */
  top: 0;
  width: 34px;
  height: 34px;
  cursor: pointer;
  background: url(~assets/images/captcha_bg.png) 0 -437px;
  background-size: 34px 471px;
}
</style>

